package com.bkgy.controller.allprocess.service.impl;


import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bkgy.controller.allprocess.dto.*;
import com.bkgy.controller.allprocess.entity.*;
import com.bkgy.controller.allprocess.mapper.AllProcessIndexMapper;
import com.bkgy.controller.allprocess.service.AllProcessIndexService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


@Service

public class AllProcessIndexServiceImpl implements AllProcessIndexService {

    @Autowired
    private AllProcessIndexMapper allProcessindexMapper;

    /**
     * 全流程成材率模型首页 总成材率
     * @param request_get AllpricessIndexReasonRequst
     * @return AllProcessIndexDetails
     */
    public  ResultData<AllProcessIndexDetails> allprocessIndex (AllpricessIndexReasonRequst request_get) {
        //日期转换与赋值
        AllpricessIndexReasonRequst request = setdatetime(request_get);

        try
        {
            Page<?> page = new Page<>(request.getPageIndex(), request.getPageSize());

            //总行数数
            int count = 5;

            // 构建响应对象
            AllProcessIndexDetails indexdetails = new AllProcessIndexDetails();

            //总成材率=冷轧成材率 + 镀锌成材率
            //全流程成材率模型首页数据-冷轧成材率（酸轧到两个连退线的成材率）
            List<TbAllpricessIndexEntity> results_totalYield = new ArrayList();

            //全流程成材率模型首页数据-冷轧成材率（酸轧到1630连退线的成材率）
            List<TbAllpricessIndexEntity> results_totalYield_cal1630 = allProcessindexMapper.getTotalYield_cal1630(page, request);
            if (!results_totalYield_cal1630.isEmpty()) {
                results_totalYield.add(results_totalYield_cal1630.get(0));
            }

            //全流程成材率模型首页数据-冷轧成材率（酸轧到2150连退线的成材率）
            List<TbAllpricessIndexEntity> results_totalYield_cal2150 = allProcessindexMapper.getTotalYield_cal2150(page, request);
            if (!results_totalYield_cal2150.isEmpty()) {
                results_totalYield.add(results_totalYield_cal2150.get(0));
            }

            /*三冷冷轧总成材率=酸轧-两个连退线*/
            TbAllpricessIndexEntity entity_CalYield = new TbAllpricessIndexEntity();
            BigDecimal Calznyield = new BigDecimal(0);//三冷冷轧 总成材率
            BigDecimal Calex_wt = new BigDecimal(0);//三冷冷轧 出口重量
            BigDecimal Calen_wt = new BigDecimal(0);//三冷冷轧 入口重量
            if (!results_totalYield_cal1630.isEmpty()) {
                Calex_wt = Calex_wt.add(results_totalYield_cal1630.get(0).getExwt());//出口重量
                Calen_wt = Calen_wt.add(results_totalYield_cal1630.get(0).getEnwt());//入口重量
            }

            if (!results_totalYield_cal2150.isEmpty()) {
                Calex_wt = Calex_wt.add(results_totalYield_cal2150.get(0).getExwt());//出口重量
                Calen_wt = Calen_wt.add(results_totalYield_cal2150.get(0).getEnwt());//入口重量
            }
            if (Calen_wt.compareTo(new BigDecimal("0")) != 0) {
                Calznyield = Calex_wt.divide(Calen_wt, 4, BigDecimal.ROUND_HALF_UP);
            }

            Calznyield = Calznyield.multiply(new BigDecimal(100));
            entity_CalYield.setLinename("cal");
            entity_CalYield.setTotalznyield(Calznyield);
            entity_CalYield.setTargetvalue(Convert.toBigDecimal(0.9444).multiply(new BigDecimal(100)));
            results_totalYield.add(entity_CalYield);
            indexdetails.setTotalYield(results_totalYield);

            //全流程成材率模型首页数据-镀锌成材率（酸轧到镀锌线成材率）
            List<TbAllpricessIndexEntity> results_totalYield_cgl = allProcessindexMapper.getTotalYield_cgl(page, request);
            if (!results_totalYield_cgl.isEmpty()) {
                results_totalYield.add(results_totalYield_cgl.get(0));
            }

            //遍历计算总成材率
            TbAllpricessIndexEntity entity_totalYield = new TbAllpricessIndexEntity();
            BigDecimal totalznyield = new BigDecimal(0);//总成材率
            BigDecimal ex_wt = new BigDecimal(0);//出口重量
            BigDecimal en_wt = new BigDecimal(0);//入口重量
            if (!results_totalYield_cal1630.isEmpty()) {
                ex_wt = ex_wt.add(results_totalYield_cal1630.get(0).getExwt());//出口重量
                en_wt = en_wt.add(results_totalYield_cal1630.get(0).getEnwt());//入口重量
            }
            if (!results_totalYield_cal2150.isEmpty()) {
                ex_wt = ex_wt.add(results_totalYield_cal2150.get(0).getExwt());//出口重量
                en_wt = en_wt.add(results_totalYield_cal2150.get(0).getEnwt());//入口重量
            }
            if (!results_totalYield_cgl.isEmpty()) {
                ex_wt = ex_wt.add(results_totalYield_cgl.get(0).getExwt());//出口重量
                en_wt = en_wt.add(results_totalYield_cgl.get(0).getEnwt());//入口重量
            }
            if (en_wt.compareTo(new BigDecimal("0")) != 0) {
                totalznyield = ex_wt.divide(en_wt, 4, BigDecimal.ROUND_HALF_UP);
            }

            totalznyield = totalznyield.multiply(new BigDecimal(100));
            entity_totalYield.setLinename("all");
            entity_totalYield.setTotalznyield(totalznyield);
            entity_totalYield.setTargetvalue(Convert.toBigDecimal(0.9415).multiply(new BigDecimal(100)));
            results_totalYield.add(entity_totalYield);
            indexdetails.setTotalYield(results_totalYield);

            return ResultData.ResultDataSuccess(indexdetails, count);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultData.ResultDataFaultSelf("获取失败" + e.getMessage(), null);
        }
    }

    /**
     * 根据前台传入查询开始和查询结束日期，构造出yyyymmddhhmmss数据查询
     * @param request_get AllpricessIndexReasonRequst
     * @return TbAllpricessIndexEntity
     */
    public AllpricessIndexReasonRequst setdatetime(AllpricessIndexReasonRequst request_get) {
        try {
            String[] array_startTime = request_get.getDbegin().split("-");//时间格式：2025-06-01 00:00:01
            String startTime = "";//时间格式：2025/6/01 00:00:01
            String dbegin_Times = "";//时间格式：20250703075254
            if (array_startTime.length > 0) {
                String month = array_startTime[1];
                if (!month.equals("10")) {
                    month = array_startTime[1].replace("0", "");
                }

                //构建时间格式：2025/6/01 00:00:01
                startTime = array_startTime[0] + "/" + month + "/" + array_startTime[2];

                //构建时间格式：20250703075254
                String times2 = array_startTime[2].replace(" ", "");
                times2 = times2.replace(":", "");
//                dbegin_Times = array_startTime[0] + array_startTime[1] + times2;
                if(request_get.getDbegin()!=null && request_get.getDbegin()!=""){
                    dbegin_Times = request_get.getDbegin().substring(0,10);
                }
                else {
                    dbegin_Times = "2025-10-01";
                }
            }
            request_get.setDbegin_Rcl(startTime);//时间格式：2025/6/01 00:00:01
            request_get.setDbegin(dbegin_Times);//时间格式：20250703075254

            String[] array_endTime = request_get.getDend().split("-");//时间格式：2025-06-01 00:00:01
            String endTime = "";//时间格式：2025/6/01 00:00:01
            String endTime_Times="";//时间格式：20250703075254
            if (array_endTime.length > 0) {
                String month = array_endTime[1];
                if(!month.equals("10"))
                {
                    month= array_endTime[1].replace("0","");
                }
                //构建时间格式：2025/6/01 00:00:01
                endTime = array_endTime[0] + "/" + month + "/" + array_endTime[2];

                //构建时间格式：20250703075254
                String times2=array_endTime[2].replace(" ","");
                times2 = times2.replace(":","");
                endTime_Times=array_endTime[0]+array_endTime[1]+times2;
                if(request_get.getDend()!=null && request_get.getDend()!=""){
                    endTime_Times = request_get.getDend().substring(0,10);
                }
                else {
                    endTime_Times = "2025-10-01";
                }
            }
            request_get.setDend_Rcl(endTime);
            request_get.setDend(endTime_Times);//时间格式：20250703075254

        } catch (Exception e) {
            e.printStackTrace();
        }
        return request_get;
    }

}
